package org.peoplesearch.pseindexer;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.peoplesearch.pseindexer.peopleretrieval.FileIterator;
import org.peoplesearch.pseindexer.processors.BaseInformationProcessor;
import org.peoplesearch.pseindexer.processors.UserProcessor;
import org.peoplesearch.pseindexer.processors.WallAdderProcessor;
import org.peoplesearch.vk.utils.SerializableUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Indexer {

    final static Logger logger = LoggerFactory.getLogger(Indexer.class);

    private FileIterator iterator;
    private final SolrServer solrServer;
    List<UserProcessor> processorList = new ArrayList<UserProcessor>();

    public static void main(String[] args) {
        Indexer indexer = new Indexer(new FileIterator("/data/temp"), "http://localhost:8983/apollo/");
        indexer.index();
    }

    public Indexer(FileIterator fileIterator, String url) {
        solrServer = new HttpSolrServer(url);
        this.iterator = fileIterator;
        processorList.add(new BaseInformationProcessor());
        processorList.add(new WallAdderProcessor());
    }

    public void index() {
        try {
            logger.debug("cleaning");
            System.out.println("Delete "+solrServer.deleteByQuery("uid:32369").getResponse());
            System.out.println("Commit "+solrServer.commit(true,true,false).getResponse());
            logger.debug("cleaned");
             int i = 0;
            while (iterator.hasNext()) {
                SolrInputDocument doc = new SolrInputDocument();
                SerializableUser user = iterator.next();
                for (UserProcessor processor : processorList) {
                    processor.process(doc, user);
                }
                solrServer.add(doc);
                logger.debug("" + (i++));
                if (i % 100 == 0){
                    solrServer.commit();
                }
            }
            logger.debug("Commiting");
            solrServer.commit();
            logger.debug("Commiteed");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SolrServerException e) {
            throw new RuntimeException(e);
        }

    }

}
